/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import type.Month;

/**
 *
 * @author wei
 */
@Entity(name="SalesForecast")
public class SalesForecastEntity extends DataObjectEntity implements Serializable {
    private int yearOfSales;
    private Month monthOfSales;
    
    @ManyToOne
    private ExpectedYearOnYearGrowthEntity expectedYoYGrowth; //input from user
    private long previousYearSales;
    private long previousPromoSales;
    private long previousYearBase; //calculated from salesOrderReport: sales-promo    
    private long baseProjection; //caclulated: previousYearBase*expectedYoYGrowth
    private long adjustedBaseProjection;
    
    private long classicBarForecast; // for sop
    private long premiumBarForecast; // for sop
    
    private long expectedMonthlyPromo; //input from user
    private double monthlyGrowthAdjustment;
    private long forecast;//baseProjection - expectedMonthlyPromo
    
    @OneToMany(cascade={CascadeType.PERSIST},mappedBy="salesForecast")
    private Collection<ProductPercentageQuantityEntity> historicalPPQ 
            = new ArrayList<ProductPercentageQuantityEntity>(); 
    //calculated from salesOrderReport. For recommendation in mps

    @OneToOne(mappedBy="salesForecast")
    private MasterProductionScheduleMonthlyEntity mpsMonthly;
    
    public SalesForecastEntity() {
        super();
    }
    
    public void setClassicBarForecast(long quantity){
        classicBarForecast = quantity;
    }
    
    public long getClassicBarForecast(){
        Iterator it = historicalPPQ.iterator();
        long quantity = 0;
        while(it.hasNext()){
            ProductPercentageQuantityEntity ppq = (ProductPercentageQuantityEntity) it.next();
            if(ppq.getProduct().getProductName().equals("Classic Nutri Bar (Nuts)")){
                quantity += forecast*ppq.getPercentage();
//                System.out.println("percentage A =" + ppq.getPercentage());
            }
            if(ppq.getProduct().getProductName().equals("Classic Nutri Bar (Fruits)")){
                quantity += forecast*ppq.getPercentage();
//                System.out.println("percentage B =" + ppq.getPercentage());
            }
        }
      classicBarForecast = quantity;
      return classicBarForecast;
    }
    
    public void setPremiumBarForecast(long quantity){
        premiumBarForecast = quantity;
    }
    
    public long getPremiumBarForecast(){
        Iterator it = historicalPPQ.iterator();
        long quantity = 0;
        while(it.hasNext()){
            ProductPercentageQuantityEntity ppq = (ProductPercentageQuantityEntity) it.next();
            if(ppq.getProduct().getProductName().equals("Premium Power Bar (Chocolate Nuts)")){
                quantity += forecast*ppq.getPercentage();
//                System.out.println("percentage C =" + ppq.getPercentage());
            }
            if(ppq.getProduct().getProductName().equals("Premium Power Bar (Redcurrant Fruits)")){
                quantity += forecast*ppq.getPercentage();
//                System.out.println("percentage D =" + ppq.getPercentage());
            }
        }
     premiumBarForecast = quantity;
      return premiumBarForecast;
    }
    
    public long getAdjustedBaseProjection(){
        
        double growth = getExpectedYoYGrowth().getGrowthRate();
        double monthlyGrowth = getMonthlyGrowthAdjustment();
                
        long previousYearBase2 = getPreviousYearBase();
        
        if(growth>=0){
            
            adjustedBaseProjection = (long)(previousYearBase2*(100+growth)/100);
            
        }
        else{
            adjustedBaseProjection = (long)(previousYearBase2*(100-growth)/100);
        }
        
        if(monthlyGrowth>=0){
            adjustedBaseProjection = (long)(adjustedBaseProjection*(100+monthlyGrowth)/100);
        }
        else{
            adjustedBaseProjection = (long)(adjustedBaseProjection*(100-monthlyGrowth)/100);
        }
        return adjustedBaseProjection;
    }
    
    public void setAdjustedBaseProjection(long projection){
        adjustedBaseProjection = projection;
    }
    
    public double getMonthlyGrowthAdjustment(){
        return monthlyGrowthAdjustment;
    }
    
    public void setMonthlyGrowthAdjustment(double number){
       
       monthlyGrowthAdjustment = number;
    }
    
    public long getPreviousYearSales(){
        return previousYearSales;
    }
    
    public void setPreviousYearSales(long previousYearSales){
        this.previousYearSales = previousYearSales;
    }
    
    public long getPreviousPromoSales(){
        return previousPromoSales;
    }
    
    public void setPreviousPromoSales(long previousPromoSales){
        this.previousPromoSales= previousPromoSales;
    }

    public long getBaseProjection() {
        
        return baseProjection;
    }

    public void setBaseProjection(long baseProjection) {
        this.baseProjection = baseProjection;
    }

    public long getExpectedMonthlyPromo() {
        return expectedMonthlyPromo;
    }

    public void setExpectedMonthlyPromo(long expectedMonthlyPromo) {
        this.expectedMonthlyPromo = expectedMonthlyPromo;
    }

    public ExpectedYearOnYearGrowthEntity getExpectedYoYGrowth() {
        return expectedYoYGrowth;
    }

    public void setExpectedYoYGrowth(ExpectedYearOnYearGrowthEntity expectedYoYGrowth) {
        this.expectedYoYGrowth = expectedYoYGrowth;
    }

    public long getForecast() {
        
       forecast = adjustedBaseProjection + getExpectedMonthlyPromo();
      
       return forecast;
    }

    public void setForecast(long forecast) {
        this.forecast = forecast;
    }

    public Collection<ProductPercentageQuantityEntity> getHistoricalPPQ() {
        return historicalPPQ;
    }

    public void setHistoricalPPQ(Collection<ProductPercentageQuantityEntity> historicalPPQ) {
        this.historicalPPQ = historicalPPQ;
    }

    public Month getMonthOfSales() {
        return monthOfSales;
    }

    public void setMonthOfSales(Month monthOfSales) {
        this.monthOfSales = monthOfSales;
    }

    public long getPreviousYearBase() {
        return previousYearBase;
    }

    public void setPreviousYearBase(long previousYearBase) {
        this.previousYearBase = previousYearBase;
    }

    public int getYearOfSales() {
        return yearOfSales;
    }

    public void setYearOfSales(int yearOfSales) {
        this.yearOfSales = yearOfSales;
    }

    public MasterProductionScheduleMonthlyEntity getMpsMonthly() {
        return mpsMonthly;
    }

    public void setMpsMonthly(MasterProductionScheduleMonthlyEntity mpsMonthly) {
        this.mpsMonthly = mpsMonthly;
    }

    @Override
    public String childToString() {
        return "SalesForecastEntity{" + "yearOfSales=" + yearOfSales + ", monthOfSales=" + monthOfSales + ", expectedYoYGrowth=" + expectedYoYGrowth + ", previousYearBase=" + previousYearBase + ", baseProjection=" + baseProjection + ", expectedMonthlyPromo=" + expectedMonthlyPromo + ", forecast=" + forecast + ", historicalPPQ=" + historicalPPQ + ", mpsMonthly=" + mpsMonthly + '}';
    }

   
    
}
